package com.ls.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.common.DataGridView;
import com.ls.common.PageModel;
import com.ls.common.ResultObj;
import com.ls.common.TreeNode;
import com.ls.system.entity.SysUser;
import com.ls.system.entity.SysMenu;

import com.ls.system.service.SysMenuService;
import com.ls.system.service.SysRoleMenuService;
import com.ls.common.utils.WebUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ls
 * @since 2025-04-06
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysRoleMenuService roleMenuService;

    @GetMapping("/loadIndexLeftMenuJson")
    public DataGridView loadIndexLeftMenuJson(){
        //结合当前登录用户的角色来加载对应的权限
        SysUser user = (SysUser) WebUtils.getSession().getAttribute("user");
        List<SysMenu> list = this.menuService.getMenusByUserId(user.getId());
        //将list转成TreeNode
        List<TreeNode> treeNodeList = new ArrayList<>();
        for (SysMenu sysMenu : list) {
            if(sysMenu.getPid().equals(0)){
                TreeNode parent = new TreeNode();
                org.springframework.beans.BeanUtils.copyProperties(sysMenu, parent);
                treeNodeList.add(parent);
                List<TreeNode> children = new ArrayList<>();
                for (SysMenu permission : list) {
                    if(parent.getId().equals(permission.getPid())){
                        TreeNode child = new TreeNode();
                        BeanUtils.copyProperties(permission, child);
                        children.add(child);
                    }
                }
                parent.setChildren(children);
            }
        }
        return new DataGridView(treeNodeList);
    }

    @GetMapping("/tree")
    public DataGridView tree(){
        List<SysMenu> list = this.menuService.list();
        List<TreeNode> treeNodeList = new ArrayList<>();
        for (SysMenu m : list) {
            TreeNode treeNode = new TreeNode(m.getId(), m.getPid(), m.getTitle(), false);
            treeNodeList.add(treeNode);
        }
        return new DataGridView(treeNodeList);
    }

    @GetMapping("/list")
    public DataGridView list(PageModel pageModel, @RequestParam(required = false) Integer pid) {
        Page<SysMenu> page = new Page<>(pageModel.getPage(), pageModel.getLimit());
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        if (pid != null) {
            queryWrapper.eq("pid", pid);
        }
        Page<SysMenu> resultPage = this.menuService.page(page, queryWrapper);
        return new DataGridView(resultPage.getTotal(), resultPage.getRecords());
    }

    @RequestMapping("/add")
    public ResultObj add(@RequestBody SysMenu sysMenu, HttpServletRequest request) {
        // 参数校验
        if(sysMenu.getPid() == null) {
            return ResultObj.ADD_ERROR;
        }

        // 确保type为menu
        sysMenu.setType("menu");

        boolean save = menuService.save(sysMenu);
        return save ? ResultObj.ADD_SUCCESS : ResultObj.ADD_ERROR;
    }

    @RequestMapping("/update")
    public ResultObj update(SysMenu sysMenu){
        boolean updateById = this.menuService.updateById(sysMenu);
        if(updateById) return ResultObj.UPDATE_SUCCESS;
        return ResultObj.UPDATE_ERROR;
    }

    @RequestMapping("/delete")
    public ResultObj delete(Integer id){
        boolean removeById = this.menuService.removeById(id);
        if(removeById) return ResultObj.DELETE_SUCCESS;
        return ResultObj.DELETE_ERROR;
    }

    @RequestMapping("/loadMenuManagerLeftTreeJson")
    public DataGridView loadMenuManagerLeftTreeJson(){
        List<SysMenu> menuList = this.menuService.list();
        List<TreeNode> treeNodeList = new ArrayList<>();
        for (SysMenu sysMenu : menuList) {
            TreeNode treeNode = new TreeNode(sysMenu.getId(), sysMenu.getPid(), sysMenu.getTitle(), false);
            treeNodeList.add(treeNode);
        }
        return new DataGridView(treeNodeList);
    }

    @RequestMapping("/checkMenuHasChildrenNode")
    public Map<String, Boolean> checkMenuHasChildrenNode(Integer id){
        Map<String, Boolean> map = new HashMap<>();
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",id);
        List<SysMenu> list = this.menuService.list(queryWrapper);
        if (list.size() > 0){
            map.put("value",true);
        }else {
            map.put("value",false);
        }
        return map;
    }

}